/**
 * Created by yangl on 2017/8/13.
 */
var productController={
    typeId:null,
    params:{},
    searchCondition:"",
      init:function(){
          //默认显示 推荐产品
          productController.params = {
              isrecom:1,
              status:0
          };
         //初始化
          productController.initPage();

          //点击搜索按钮
          $("#searchbutton").click(function(){
              productController.params = {};
              productController.initPage();
          });
      },
    initPage:function(){
        var data = {
            page:1,
            rows:pageRows,
            params:productController.params
        };
        $.ajax({
            type: "post",
            url: "/custom/product/data/list",
//                cache:false,
//                async:false,
            dataType: "json",
            data:data,
            success: function(json){
                var pageCount = Math.ceil(json.totalData/pageRows);
                $('.M-box4').pagination({
                    isHide:true,
                    coping:true,//是否开启首页和末页，值为boolean
                    homePage:'首页',
                    endPage:'末页',
                    prevContent:'上页',
                    nextContent:'下页',
                    count:1,//当前选中页前后页数
                    pageCount: pageCount,//总页数
                    jump: true,//是否开启跳转到指定页数，值为boolean类型
                    callback:function(api){
                        productController.buildData(api);
                    }
                });
                $("#productlist").html("");
                if(json.rows.length>0){
                    $(".M-box4").show();
                    $("#nodatatip").hide();
                    productController.showHtmlData(json.rows);
                }
            },
            failure:function (result) {
                console.log("加载产品信息失败");
            }
        });
    },
    buildData:function(api){
        var params = productController.params;
        var searchCondition = $("#searchCondition").val();
        if(searchCondition){
            params.searchCondition = searchCondition;
        }else{
            params = {
                isrecom:1,
                status:0
            };
            productController.params = params;
        }
        params.typeId = "";
        if(productController.typeId){
            params.typeId = productController.typeId;
        }
        productController.initData(api,params);
    },
    initData:function(api, params){
        var data = {
            page:api.getCurrent(),
            rows:pageRows,
            params:params
        };
        $.ajax({
            type: "post",
            url: "/custom/product/data/list",
//                cache:false,
//                async:false,
            dataType: "json",
            data:data,
            success: function(json){
                $("#productlist").html("");
                if(json.rows.length>0){
                    $(".M-box4").show();
                    $("#nodatatip").hide();
                    productController.showHtmlData(json.rows);
                }
                // else{
                //     $(".M-box4").hide();
                //     $("#nodatatip").show();
                // }
            },
            failure:function (result) {
                console.log("加载产品信息失败");
            }
        });
    },
    showHtmlData:function(data){
        $.each(data, function(i,n){
            $("#productlist").append(productController.buildHtml(n));
        });
    },
    buildHtml:function(n){
        var picpath = '/static/custom/images/sp_img.png';
        if(n.picFirstId){
            picpath = '/fileDownLoad/showFile/'+n.picFirstId;
        }

        var htmldata = '<li>'
            +' <div class="sp-dataBox goods-box">'
            //商品链接图和名
            +' <a href="/custom/product/page/detail/'+n.id+'" target="_blank">'
            +'  <img src="'+picpath+'" class="userImg" alt=""/>'
            +' <h3>'+n.name+'</h3>'
            +' </a>'
            // +'  <div class="list-info">'
            // +' <small class="production">'+n.content+'</small>'
            // +' </div>'
            +' <div class="charge">'
            +' <p>￥'+n.price+'<small>元</small></p>'
            +' </div>'
            +' </div>'
            +' </li>';
        return htmldata;
    },
    clicktype:function(typeId){
        productController.typeId=typeId;
        productController.initPage();
    }
}

$(function(){
    productController.init();
})
